//
//  HJFeedbackController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJFeedbackController.h"

@interface HJFeedbackController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *feedBackText;
@property (weak, nonatomic) IBOutlet UILabel *codeLabel;
@property (weak, nonatomic) IBOutlet UILabel *hideLabel;
@property (weak, nonatomic) IBOutlet UIButton *btmBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btmY;
@end

@implementation HJFeedbackController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self.feedBackText.rac_textSignal filter:^BOOL(NSString*text){
        return text.length >= 100;
    }] subscribeNext:^(NSString *text){
        self.feedBackText.text = [text substringToIndex:100];
    }];
    
    self.btmBtn.layer.shadowOpacity = 0.2;
    self.btmBtn.layer.shadowRadius = 3;
    self.btmBtn.layer.shadowOffset = CGSizeZero;
    self.btmBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    if (mScreenHeight==812.0f) {
        self.btmY.constant =34.0f;
    }else {
       self.btmY.constant = 0.0f;
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    self.hideLabel.hidden = YES;
}

//正在改变
- (void)textViewDidChange:(UITextView *)textView {
    
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length - range.length + text.length > 100) {
        return NO;
    }else {
         self.codeLabel.text = [NSString stringWithFormat:@"还可以输入%zd字",(100 - (textView.text.length - range.length + text.length))];
        return YES;
    }
}
-(void)loadTextWork {
    [TZHttpTool post:OpinionAPI params:@{@"content":self.feedBackText.text} success:^(NSDictionary *result) {
        [self showSuccessHUDWithStr:@"感谢您的参与"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

- (IBAction)okbutton:(UIButton *)sender {
    if ([self.feedBackText.text isEqualToString:@""]) {
        [self showErrorHUDWithError:@"请输入意见反馈内容"];
        return;
    }
    [self loadTextWork];
}
@end
